﻿using System;

namespace IFDB.ResourceLibrary
{
    public static class Extensions
    {
        public static DateTime StartOfDay(this DateTime dateValue)
        {
            return new DateTime(dateValue.Year, dateValue.Month, dateValue.Day);
        }

        public static DateTime EndOfDay(this DateTime dateValue)
        {
            return StartOfDay(dateValue).AddDays(1).AddMilliseconds(-1);
        }

        public static string DayPrefix(this DateTime dateValue)
        {
            switch (dateValue.Day)
            {
                case 1:
                case 21:
                case 31:
                    return dateValue.Day.ToString() + "st";
                
                case 2:
                case 22:
                    return dateValue.Day.ToString() + "nd";

                case 3:
                case 23:
                    return dateValue.Day.ToString() + "rd";

                default:
                    return dateValue.Day.ToString() + "th";
            }
        }

        public static bool IsBetween(this DateTime test, DateTime startDate, DateTime endDate)
        {
            return test >= startDate && test <= endDate;
        }

        public static string GetTextBetween(this string fullText, string start, string end)
        {
            string returnValue = string.Empty;
            if (fullText.IndexOf(start) != -1 && fullText.IndexOf(end) != -1)
            {
                returnValue = fullText.Substring(fullText.IndexOf(start) + start.Length);
                returnValue = returnValue.Substring(0, returnValue.IndexOf(end));
            }
            return returnValue;
        }

        public static string GetTextBefore(this string fullText, string end)
        {
            return fullText.IndexOf(end) != -1 ? fullText.Substring(0, fullText.IndexOf(end)) : string.Empty;
        }

        public static string GetTextAfter(this string fullText, string start)
        {
            return fullText.IndexOf(start) != -1 ? fullText.Substring(fullText.IndexOf(start) + start.Length) : string.Empty;
        }
    }
}
